//
//  OrderTapGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/13/13.
//
//

#include "OrderTapGameScene.h"

#include "MyUtilities.h"
#include "SceneManager.h"
#include "GameLayerManager.h"

CCScene* OrderTapGameScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    OrderTapGameScene *layer = OrderTapGameScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->orderTapGameLayer = layer;
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;

}

bool OrderTapGameScene::init(){
    CountDownGameScene::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    buttonArray = CCArray::create();
    buttonArray->retain();
    double buttonSize = winSize.width * 0.3;
    double offsetX = (winSize.width - 2*buttonSize)/3;
    double offsetY = (winSize.height - (offsetX+2*buttonSize))/2;
    
    for(int i=0;i<4;i++){
        int row = i/2;
        int column = i%2;
        MyDisabledTextButton* iButton = MyDisabledTextButton::create("circle_button.png", (column+1)*offsetX+column*buttonSize, winSize.height-scoreAndTimeLayer->getBarHeight()- offsetY-((row)*offsetX + (row+1)*buttonSize), buttonSize, buttonSize);
        CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(OrderTapGameScene::onTapButton), CCInteger::create(i));
        iButton->label->setString(MyUtilities::toString(i+1));
        iButton->addCallback(callback);
        if(i==0){
            iButton->setEnable(true);
        }else{
            iButton->setEnable(false);
        }
        
        this->addChild(iButton);
        buttonArray->addObject(iButton);
    }
    
    isLock = false;
    return true;
}

OrderTapGameScene* OrderTapGameScene::create(int numberOfSeconds){
    OrderTapGameScene* scene = OrderTapGameScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}
void OrderTapGameScene::onTapButton(cocos2d::CCObject *sender){
    int tapIndex = ((CCInteger*)sender)->getValue();
    numberOfTap++;
    scoreAndTimeLayer->updateScore(numberOfTap);
    MyDisabledTextButton* tappedButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(tapIndex);
    tappedButton->setEnable(false);
    int nextIndex = tapIndex+1;
    if(nextIndex>=buttonArray->count()){
        nextIndex=0;
    }
    currentAvailableButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(nextIndex);

    currentAvailableButton->setEnable(true);

    
}

void OrderTapGameScene::startGame(){
    CountDownGameScene::startGame();
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->orderTapGameScene;
}

void OrderTapGameScene::reset(){
    CountDownGameScene::reset();
    for(int i=0;i<buttonArray->count();i++){
        MyDisabledTextButton* iButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(i);
        if(i==0){
            currentAvailableButton = iButton;
            iButton->setEnable(true);
        }else{
            iButton->setEnable(false);
        }
    }
    this->unschedule(schedule_selector(OrderTapGameScene::tickTimeLock));
    isLock=false;
    
}

void OrderTapGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if (isLock) {
        return;
    }
    for(int i=0;i<buttonArray->count();i++){
        MyDisabledTextButton* iButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(i);
        if(iButton->MyButton::isTouchedAt(touches)){
            if(iButton->isEnabled){
                iButton->runAction(CCSequence::create(iButton->callback,NULL));
                iButton->isFocused=false;
            }else{
                lock();
                currentAvailableButton->goToHighlightMode();
                
            }
            
        }
    }
}

void OrderTapGameScene::lock(){
    isLock = true;
    this->schedule(schedule_selector(OrderTapGameScene::tickTimeLock), 1, 0, 0);
}

void OrderTapGameScene::tickTimeLock(float dt){
    this->unschedule(schedule_selector(OrderTapGameScene::tickTimeLock));
    isLock=false;
    currentAvailableButton->goToNormalMode();
}

void OrderTapGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
//    for(int i=0;i<buttonArray->count();i++){
//        MyDisabledTextButton* iButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(i);
//        iButton->handleMultiTouchesMoved(touches, pEvent);
//    }
}
void OrderTapGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
//    for(int i=0;i<buttonArray->count();i++){
//        MyDisabledTextButton* iButton = (MyDisabledTextButton*)buttonArray->objectAtIndex(i);
//        iButton->handleMultiTouchesEnded(touches, pEvent);
//    }
}